package com.google.code.lf.gfm.ui.log;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.runtime.IStatus;

import com.google.code.lf.gfm.ui.error.StatusHelper;

//TODO Javadoc
public class LogHelper {

	private static final Logger logger = Logger.getLogger(LogHelper.class.getClass().getPackage().getName());

	private LogHelper() {
		throw new UnsupportedOperationException("Classe utilitaire. Ne doit pas être instanciée.");
	}

	/**
	 * Méthode nécessaire pour pallier au pb de configuration du java.util.Logger par fichier de configuration dans Eclipse RCP.
	 * <br/>Cette méthode doit être appelée au démarrage du plugin.
	 */
	public static void configure() {
		final ConsoleHandler consoleHandler = new ConsoleHandler();
		consoleHandler.setFormatter(new LogFormatter());
		logger.addHandler(consoleHandler);
		return;
	}

	private static void log(final String message, final Level logLevel, final Throwable throwable) {
		if (logger.isLoggable(logLevel)) logger.log(logLevel, message, throwable);
	}

	public static void log(final String message, final IStatus status, final Throwable throwable) {
		log(message, StatusHelper.toLogLevel(status.getSeverity()), throwable);
	}

	public static void trace (final String message) {
		trace(message, null);
	}

	public static void trace (final String message, final Throwable throwable) {
		log(message, Level.FINEST, throwable);
	}

	public static void debug (final String message) {
		debug(message, null);
	}

	public static void debug (final String message, final Throwable throwable) {
		log(message, Level.FINE, throwable);
	}

	public static void info (final String message) {
		info(message, null);
	}

	public static void info (final String message, final Throwable throwable) {
		log(message, Level.INFO, throwable);
	}

	public static void warn (final String message) {
		warn(message, null);
	}

	public static void warn (final String message, final Throwable throwable) {
		log(message, Level.WARNING, throwable);
	}

	public static void fatal (final String message) {
		fatal(message, null);
	}

	public static void fatal (final String message, final Throwable throwable) {
		log(message, Level.SEVERE, throwable);
	}

}
